<?php
require_once 'TaobaoApi/TopSdk.php';

class Model_Taobao extends Ac_Model
{
	protected $_sessionKey;
	protected $_c;
	protected $_req;
	protected $_data = array(
		'fields' => '',
		'resp' => ''
	);

	protected static $_host = array(
		'item' => array(
			'item.taobao.com',
			'detail.tmall.com'
		)
	);
	



    public function __construct()
	{
		//global $taobao_account;
		$taobao_account = array(
			'appkey' => '12639833',
			'secretKey' => '958615fecab40c6a31bd0cdec6c60d25',
			'sessionKey' => '6100b03e0251be45e42f49b54e3f4c7dd04c30e30340fd7150110531'
		);
		$this->_c = new TopClient;
		$this->_c->appkey = $taobao_account['appkey'];
		$this->_c->secretKey = $taobao_account['secretKey'];
		$this->_sessionKey = $taobao_account['sessionKey'];
		$this->_c->format = 'json';
    }
    
    /***
     * only process url like _host['item']
     */
    public function getItemByUrl($url)
    {
    	$urlParsed = Model_Util::parseUrl($url);
    	if(in_array($urlParsed['host'], self::$_host['item']) && isset($urlParsed['query']['id'])){
    		$this->getItemById($urlParsed['query']['id']);
    	}else{
			//todo something else
    	}
    	return $this->resp;
    }
    
    public function getItemById($id)
    {
    	$this->_req = new ItemGetRequest;
		$this->_req->setNumIid($id);
		$this->_req->setFields($this->fields);
		$this->reqExecute();
		return $this->resp; 
    }
    
    public function setFields($fields = ""){
    	$this->fields = empty($fields)? 
    		"detail_url,num_iid,title,nick,type,cid,seller_cids,props,input_pids,input_str,desc,pic_url,num,valid_thru,list_time,delist_time,stuff_status,location,price,post_fee,express_fee,ems_fee,has_discount,freight_payer,has_invoice,has_warranty,has_showcase,modified,increment,approve_status,postage_id,product_id,auction_point,property_alias,item_img,prop_img,sku,video,outer_id,is_virtual"
    		: $fields;
    }
    
    private function reqExecute()
    {
    	$this->resp = $this->_c->execute($this->_req, $this->_sessionKey);
    }
    
}
